package com.share.PACManager.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.share.PACManager.model.PoliceModel;
import com.share.PACManager.model.TerminalModel;
import com.share.PACManager.net.HTTPResponse;
import com.share.PACManager.net.HttpUtility;
import com.share.PACManager.utils.Cryptography;
import com.share.PACManager.utils.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;


public class GetPolicesTask extends Thread{

	private GetPolicesListener mListener;
	private Context mContext;	
	private TerminalModel mTerminal;
	private List<PoliceModel> mPolices;
	
	public interface GetPolicesListener {
		public TerminalModel requestTerminal();
		public void onCheckResult(boolean result, List<PoliceModel> list);
	}

	public GetPolicesTask(Context context) {
		mContext = context;
	}
	
	public void start(GetPolicesListener listener) {
		mListener = listener;
		if(mListener != null)
		{
			mTerminal = mListener.requestTerminal();
			start();
		}
	}

	private Handler handler = new Handler(){		
		@Override
		public void handleMessage(Message msg)
		{
			if(mListener != null)
			{
				if(msg.what != 0)
					mListener.onCheckResult(false, mPolices);
				else
				{
					HTTPResponse resp = (HTTPResponse) msg.obj;
					mListener.onCheckResult(parse(resp.content), mPolices);								
				}
			}
		}
	};
	
    public boolean parse(String json)
    {
		try {
			JSONObject jsonObject = new JSONObject(json);
			if(!jsonObject.getBoolean("Success"))
				return false;
			
			JSONArray state = jsonObject.getJSONArray("State"); 
			if(state != null)
			{
				mPolices = new ArrayList<PoliceModel>();
				for(int i = 0 ; i < state.length(); i++)
				{
					JSONObject object = state.getJSONObject(i);		
					
					PoliceModel police = new PoliceModel();		
					
					police.PoliceId = Utils.getJsonString(object, "PoliceId");
					police.PoliceArea = Utils.getJsonString(object, "PoliceArea");
					police.PoliceName = Utils.getJsonString(object, "PoliceName");
					police.PolicePhoneNumber = Utils.getJsonString(object, "PolicePhoneNumber");
					police.PoliceCardImageId = Utils.getJsonString(object, "PoliceCardImageId");
					police.PoliceCardImage = Utils.getJsonString(object, "PoliceCardImage");
					if("null".equals(police.PoliceCardImage))
						police.PoliceCardImage = "";
					
					police.PoliceWorkingStatus = Utils.getJsonString(object, "PoliceWorkingStatus");
					police.PoliceCreateTime = Utils.getJsonDate(object,"PoliceCreateTime");
					police.PoliceChangeTime = Utils.getJsonDate(object,"PoliceChangeTime");
					police.PoliceComment = Utils.getJsonString(object, "PoliceComment");
					
					mPolices.add(police);
				}				
			}
			return true;			
			
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return false;
    }
    
	@Override
	public void run() {

		try {

			String terminalId = mTerminal.TerminalId;
			String imei = mTerminal.TerminalCode;
			String password = mTerminal.TerminalPassword;
			String ticket = Cryptography.AES_Encode(imei, terminalId, password);
			
			Map<String, String> params = new TreeMap<String, String>();
			params.put("Id", terminalId);
			params.put("TerminalId", terminalId);
			params.put("DataType", "Data");	
			params.put("Password", password);
			params.put("TerminalCode", imei);
			params.put("Version", "1.0");
			params.put("Ticket", ticket);
			params.put("Objective", "GetPolices");
					
			handler.obtainMessage(0, HttpUtility.httpPost(params, "")).sendToTarget();

		} catch (Exception e) {
			handler.obtainMessage(1).sendToTarget();
		}
		
		super.run();
	}
}
